(function(){
	$.fn.chart2 = function(options){
		var me = this;
		var skin = $.extend(true, {}, Ccay.UI.Chart.defaultConfig,  options.skin);
		var chart = this;
		var op = $.extend(true,{},skin,options);
		
		var type = options.convertArrayType || "1";
		//通过AJAX渲染
		if(options.ajax){
			Ccay.UI.ajax.setFnSuccess(options.ajax,function(datas){
				if(datas.result)
					datas = datas.result;
				if($.isArray(datas)){
					if(options.fnConvert){
						datas = options.fnConvert(datas);
					}else{
						datas = Ccay.DataHelper.convert2Array(datas,options.convertFields,type);
					}
				}
				//已存在则重新渲染，防止内存泄露
				var oldChartAjax = me.data("chart");
				if(oldChartAjax && oldChartAjax.replot){
					op.data = datas;
					//op.resetAxes  = true;
					oldChartAjax.replot(op);
					return;
				}
				var plotValue = chart.jqplot(datas,op);
				if(op.click){
					bindEvent(plotValue,'jqplotDataClick', op.click);
				}
				me.data("chart",plotValue.data('jqplot'));
			});
			Ccay.UI.ajax(options.ajax,me);
			
			return ;
		} 
		//直接渲染数据
		//已存在则重新渲染，防止内存泄露
		var oldChart = $(me).data("chart");
		if(oldChart && oldChart.replot){
			op.data = options.data;
			//op.resetAxes  = true;
			oldChart.replot(op);
			return;
		}
		var plotValue = chart.jqplot(options.data,op);
		if(op.click){
			bindEvent(plotValue,'jqplotDataClick', op.click)
		}
		$(me).data("chart",plotValue.data('jqplot'));
		
	};
	
	/**
	 * 生成图片
	 * @parma {jqDom} 图表所在的jqDom对象
	 * @param {jqDom} 放置渲染后的图片的jqDOM对象
	 */
	$.fn.chartAsImage = function(showView, imageContainer){
		var imgData = this.jqplotToImageStr({}); 
		if(imageContainer){
			var imgElem = $('<img/>').attr('src', imgData);
			this.append(imgElem);
		}else{
			Ccay.UI.showImage({'img':imgData,'title':{'en_US':'Right click the image below to save image.','zh_CN':'在下面图片上点击右键来另存图片'}[workspaceVO.currentLanguage]},showView);
		}
	};
	
	var bindEvent = function(chart, eventName, fn){
		if(eventName){
			chart.bind(eventName,fn);
		}
	};
	
	Ccay.DataHelper.convert2Array = function(list ,fieldArray,type){
		var result = [];
		var tmp;
		
		var count1 = list.length;
		var count2 = fieldArray.length;
		
		if(type != "1"){
			count1 = fieldArray.length;
			count2 = list.length;
		}
		
		for (var idx = 0; idx < count1; idx++) {
			tmp = [];
			for(var dIdx = 0; dIdx < count2 ;dIdx++){
				var index1 = idx;
				var index2 = dIdx;
				
				if(type != 1){
					index1 = dIdx;
					index2 = idx;
				}
				if(typeof fieldArray[index2] == "function"){
					tmp.push(fieldArray[index2](list[index1]));
				}else{
					tmp.push(list[index1][fieldArray[index2]]);
				}
			}
			result.push(tmp);
		}
		return result;
	};
	$.jqplot.Cursor.prototype.resetZoom = function(u, v) {
		var t = u.axes;
		var s = v._zoom.axes;
		if (!u.plugins.cursor.zoomProxy && v._zoom.isZoomed) {
			for (var r in t) {
				t[r].reset();
				t[r]._ticks = [];
				t[r]._autoFormatString = s[r].tickFormatString
			}
			u.redraw();
			v._zoom.isZoomed = false
		} else if(v.zoomCanvas){
			var q = v.zoomCanvas._ctx;
			q.clearRect(0, 0, q.canvas.width, q.canvas.height);
			q = null
		}
		u.target.trigger("jqplotResetZoom", [u, v])
	};

	/**
	 * Chart 名字空间
	 */
	Ccay.UI.Chart = {
		/**
		 * 默认配色
		 */
		defaultConfig : {
			animate: true,
			//animateReplot: true,
			seriesColors : ['#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'],
			axesDefaults : {
					tickOptions : {
					showGridline : false,
					showMark:false				
				},
				showTickMarks:false,
				tickDistribution: 'even'
			},
			axes : {
				yaxis : {
					labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
					tickOptions : {
						showGridline : true
					}
				}
			},
			seriesDefaults : {
				//在线上显示数据对应的小圆点
				showMarker : true,
				lineWidth : 2,
				shadow : false,
				markerOptions : {
					size : 7
				}
			},
			cursor : {
				show : false
			},
			legend : {
				location : 'ne',
				placement : 'inside'
			},
			tickOptions: { 
				show:false,
				markSize:0,
                fontFamily:'arial'                    
			},
			grid : {
				background : "#FFF",
				shadow:false,
				drawGridLines:false,
				borderWidth: 1,
				borderColor:"#000"
			}
		}	
	};
	/**
	 * 黑色配色
	 */
	Ccay.UI.Chart.blackThemeConfig = $.extend(true, {}, Ccay.UI.Chart.defaultConfig, {
			seriesColors : ["#DDDF0D", "#7798BF", "#55BF3B", "#DF5353", "#aaeeee", "#ff0066", "#eeaaee",  "#55BF3B", "#DF5353", "#7798BF", "#aaeeee"],
			grid : {
				background : "#646464",
				borderColor:"#646464"
			}
		});
	/**
	 * 默认高亮配置
	 */
	Ccay.UI.Chart.defaultHighlighter = {
			show:true,
			sizeAdjust : 10,
			tooltipLocation : 'n',
			tooltipAxes : 'y',
			tooltipFormatString : ' %.1f',
			useAxesFormatters : false		
	};
}());
